package com.qf.team2.netdisc.entity;

import org.apache.commons.lang3.RandomStringUtils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

/**
 * 验证码实体类
 *
 * @author Simon_Wong
 * @date 2021/05/26 11:32
 */
public class Code {
    public String creatUrl() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        MessageDigest m = null;
        try {
            m = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        byte[] s = new byte[0];
        if (m != null) {
            m.update(format.getBytes(StandardCharsets.UTF_8));
            s = m.digest();
        }

        StringBuilder result = new StringBuilder();
        for (byte b : s) {
            result.append(Integer.toHexString((0x000000ff & b) | 0xffffff00).substring(6));
        }
        return result.toString();
    }

    public String creatCode(){
        return RandomStringUtils.randomAlphabetic(4);
    }
}
